package com.lycx.businessmanager.service.impl;

import java.util.Date;
import java.util.List;

import com.lycx.businessmanager.constant.ConstantsDbOperation;
import com.lycx.businessmanager.constant.ConstantsMessage;
import com.lycx.businessmanager.constant.ConstantsTableName;
import com.lycx.businessmanager.dao.SupplierDao;
import com.lycx.businessmanager.manager.SequenceManager;
import com.lycx.businessmanager.model.Supplier;
import com.lycx.businessmanager.service.ISupplierService;

public class SupplierServiceImpl implements ISupplierService {

	private SupplierDao supplierDao;

	public void setSupplierDao(SupplierDao supplierDao) {
		this.supplierDao = supplierDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Supplier> selectForPage(Supplier supplier, int startNum, int limit) {
		List<Supplier> result = null;
		try {
			result = supplierDao.selectForPagination(ConstantsDbOperation.SUPPLIER_SELECT, supplier, startNum, limit);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String add(Supplier supplier) {
		String result = null;
		try {
			List<Supplier> list = selectRepeat(supplier);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.SUPPLIER_REPEAT_FAIL;
			} else {
				supplier.setId(SequenceManager.createId(ConstantsTableName.SUPPLIER));
				supplier.setCreateTime(new Date());
				supplier.setModifyTime(new Date());
				supplierDao.insert(ConstantsDbOperation.SUPPLIER_INSERT, supplier);
				result = ConstantsMessage.SUPPLIER_ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.SUPPLIER_ADD_FAIL;
		}
		return result;
	}

	@Override
	public String del(int[] ids) {
		String result = null;
		try {
			if (ids != null && ids.length > 0) {
				for (int id : ids) {
					Supplier supplier = new Supplier();
					supplier.setId(id);
					supplierDao.delete(ConstantsDbOperation.SUPPLIER_DELETE, supplier);
					deleteChildren(supplier);
				}
				result = ConstantsMessage.SUPPLIER_DEL_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.SUPPLIER_DEL_FAIL;
		}
		return result;
	}

	@Override
	public String mod(Supplier supplier) {
		String result = null;
		try {
			List<Supplier> list = selectRepeat(supplier);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.SUPPLIER_REPEAT_FAIL;
			} else {
				supplier.setModifyTime(new Date());
				supplierDao.update(ConstantsDbOperation.SUPPLIER_UPDATE, supplier);
				result = ConstantsMessage.SUPPLIER_MOD_SUCCESS;
			}

		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.SUPPLIER_MOD_FAIL;
		}
		return result;
	}

	@Override
	public int selectTotalCount(Supplier supplier) {
		return (Integer) supplierDao.selectOne(ConstantsDbOperation.SUPPLIER_SELECT_TOTAL_COUNT, supplier);
	}

	@SuppressWarnings("unchecked")
	private List<Supplier> selectRepeat(Supplier supplier) {
		return (List<Supplier>) supplierDao.select(ConstantsDbOperation.SUPPLIER_SELECT_REPEAT, supplier);
	}
	
	@SuppressWarnings("unchecked")
	private void deleteChildren(Supplier supplier) {
		List<Supplier> supplierList = supplierDao.select(ConstantsDbOperation.SUPPLIER_SELECT_CHILDREN, supplier);
		if(supplierList != null && supplierList.size() > 0){
			for(Supplier supplierTemp : supplierList){
				deleteChildren(supplierTemp);
				supplierDao.delete(ConstantsDbOperation.SUPPLIER_DELETE, supplierTemp);
			}
		}
	}

}
